<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- <div v-if="crud.props.searchToggle">
        <label class="el-form-item-label">功能标识</label>
        <el-input v-model="query.functionFlag" clearable placeholder="功能标识" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">用户名</label>
        <el-input v-model="query.username" clearable placeholder="用户名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div> -->
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="功能标识" prop="functionFlag">
            <el-input v-model="form.functionFlag" style="width: 250px;" readonly />
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <!-- <el-input v-model="form.username" style="width: 250px;" /> -->
            <el-select v-model="form.username" filterable clearable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in userList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否可以审核">
            <!-- <el-input v-model="form.canApprove" style="width: 250px;" /> -->
            <el-select v-model="form.canApprove" clearable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in isApproveList"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否可发送邮件">
            <!-- <el-input v-model="form.canSendEmail" style="width: 250px;" /> -->
            <el-select v-model="form.canSendEmail" clearable placeholder="请选择" style="width: 250px;">
              <el-option
                v-for="item in isSendEmailList"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="functionFlag" label="功能标识" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="canApprove" label="是否可以审核" />
        <el-table-column prop="canSendEmail" label="是否可发送邮件" />
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { queryUserAPI } from '@/api/work/email'
import crudUserFunctionMapping from '@/api/work/userFunctionMapping'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, functionFlag: null, username: null, canApprove: null, canSendEmail: null }
export default {
  name: 'RightEmail',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'userFunctionMapping', url: 'api/userFunctionMapping', idField: 'id', sort: 'id,desc', crudMethod: { ...crudUserFunctionMapping },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      },
      queryOnPresenterCreated: false
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'userFunctionMapping:add', 'otherUser'],
        edit: ['admin', 'userFunctionMapping:edit', 'otherUser'],
        del: ['admin', 'userFunctionMapping:del', 'otherUser']
      },
      rules: {
        functionFlag: [
          { required: true, message: '请选择功能模块标识', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'functionFlag', display_name: '功能标识' },
        { key: 'username', display_name: '用户名' }
      ],
      isApproveList: [
        {
          id: 1,
          label: '是',
          value: 1
        },
        {
          id: 2,
          label: '否',
          value: 0
        }
      ],
      isSendEmailList: [
        {
          id: 1,
          label: '是',
          value: 1
        },
        {
          id: 2,
          label: '否',
          value: 0
        }
      ],
      userList: []
    }
  },
  watch: {
    'crud.status.add'(newValue) {
      if (newValue === 1) {
        this.form.functionFlag = this.query.functionFlag
      }
    }
  },
  mounted() {
    this.queryUser()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async queryUser() {
      const res = await queryUserAPI()
      if (res.result === 'OK') {
        this.userList = res.data
      }
    }
  }
}
</script>

<style scoped>

</style>
